package com.example.designpattern.responsibility_two;

import com.example.designpattern.responsibility_two.validator.LengthValidator;
import com.example.designpattern.responsibility_two.validator.NotNullValidator;
import com.example.designpattern.responsibility_two.validator.PriceValidator;
import com.example.designpattern.responsibility_two.validator.StockValidator;
import com.example.model.GoodsDTO;
import org.springframework.beans.factory.InitializingBean;

import javax.annotation.Resource;

/**
 *  todo 将所有的实现类都注入，然后在初始化方法中组装顺序
 * @author long
 * @date 2024/12/20 14:49
 */
//@Service
public class ParamValidatorChain implements InitializingBean {

    @Resource
    private NotNullValidator firstValidator;

    @Resource
    private LengthValidator lengthValidator;

    @Resource
    private PriceValidator priceValidator;

    @Resource
    private StockValidator stockValidator;

    @Override
    public void afterPropertiesSet() throws Exception {
        firstValidator.setNext(lengthValidator)
                .setNext(priceValidator).setNext(stockValidator);
    }

    public void startParamValidate(GoodsDTO dto){

        // 链式调用校验
        firstValidator.validate(dto);

        // todo 后续的业务操作

    }

}
